package com.qianfeng.zuul_80.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * author: 田晋宇
 * datetime:2021/10/26 17:18
 * email:853796359@qq.com
 */
@Component
public class DynamicRoutingFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {


        RequestContext currentContext = RequestContext.getCurrentContext();

        HttpServletRequest request = currentContext.getRequest();

        String rediskey = request.getParameter("rediskey");

        System.out.println("动态路由进来了...............");
        if(rediskey !=null && "customer_7001".equalsIgnoreCase(rediskey)){

            currentContext.put(FilterConstants.SERVICE_ID_KEY,"customer_7001");
            currentContext.put(FilterConstants.REQUEST_URI_KEY,"/s1");
        }else if(rediskey !=null && "search".equalsIgnoreCase(rediskey)){
            currentContext.put(FilterConstants.SERVICE_ID_KEY,"search-v1");
            currentContext.put(FilterConstants.REQUEST_URI_KEY,"/s2");
        }

        return null;
    }
}
